﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;


public partial class Users_CreateNetMu : BasePage
{
    protected string DefaultValue = "";
    protected string t = "1";
    private NetMuBLL bll = new NetMuBLL();
    private SaleNumber salBll = new SaleNumber();
    AspNetCs.Bll.Temple tempBll = new Temple();
    protected void Page_Load(object sender, EventArgs e)
    {
        //CheckUserExit("Login.aspx","请您登录!",true);
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "请您登录", true);
        

        if(!IsPostBack)
        {
            int Point = (int)Base_GetUserInfo.Point;
            if (Point < configInfo.OnegravestonePoint)
            {
                //ResultStr("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "您的可用点数:" + Point + ",创建公墓至少需要" + bll.NeedPointCreateNetMu(NetMuTypeEnum.单人墓) + "点,请您及时充值");
                ResultStr("Default.Aspx", "您的可用点数:" + Point + ",创建公墓至少需要" + configInfo.OnegravestonePoint+ "点,请您及时充值");
            }
            BindKindsOfDataControl();
            t = ddlMuType.SelectedValue;
            ArrayList list = new ArrayList();
            DataTable dt = salBll.GetNetMuIdDataTable(" and SaleType=" + (int)Channel_Type.memorial + "  and  UserId=0");
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(dr["Number"]);
            }
            DefaultValue = bll.RndNum(list, RndLen);
            this.txtNetMuId.Text = DefaultValue;
        }
        
    }

    private void BindKindsOfDataControl()
    {
        EnumDescription[] Sex_Ed = EnumDescription.GetFieldTexts(typeof(Sex_Enum), EnumDescription.SortType.Rank);
        EnumDescription[] Ed = EnumDescription.GetFieldTexts(typeof(TimeType_enum), EnumDescription.SortType.Rank);

        Dictionary<string, string> dicNetMuTypes = BindEnumList<NetMuTypeEnum>.BindEnum();

        ddlMuType.DataSource = dicNetMuTypes;
        ddlMuType.DataTextField = "value";
        ddlMuType.DataValueField = "key";
        ddlMuType.DataBind();

        TwoPanel.Visible = false;

        this.Radio_Sex.DataTextField = "EnumDisplayText";
        this.Radio_Sex.DataValueField = "EnumDisplayText";
        this.Radio_Sex.DataSource = Sex_Ed;
        this.Radio_Sex.DataBind();
        this.Radio_Sex.SelectedIndex = 0;
        this.Radio_Sex.RepeatColumns = 5;

        this.ddlSexTwo.DataTextField = "EnumDisplayText";
        this.ddlSexTwo.DataValueField = "EnumDisplayText";
        this.ddlSexTwo.DataSource = Sex_Ed;
        this.ddlSexTwo.DataBind();
        this.ddlSexTwo.SelectedIndex = 0;
        this.ddlSexTwo.RepeatColumns = 5;





        this.DD_TimeType.DataTextField = "EnumDisplayText";
        this.DD_TimeType.DataValueField = "EnumDisplayText";
        this.DD_TimeType.DataSource = Ed;
        this.DD_TimeType.DataBind();
        this.DD_TimeType.SelectedIndex = 0;

        this.DD_DeathType.DataTextField = "EnumDisplayText";
        this.DD_DeathType.DataValueField = "EnumDisplayText";
        this.DD_DeathType.DataSource = Ed;
        this.DD_DeathType.DataBind();
        this.DD_DeathType.SelectedIndex = 0;




        this.Txt_birChangeTwo.DataTextField = "EnumDisplayText";
        this.Txt_birChangeTwo.DataValueField = "EnumDisplayText";
        this.Txt_birChangeTwo.DataSource = Ed;
        this.Txt_birChangeTwo.DataBind();
        this.Txt_birChangeTwo.SelectedIndex = 0;

        this.DD_DeathTimeTwo.DataTextField = "EnumDisplayText";
        this.DD_DeathTimeTwo.DataValueField = "EnumDisplayText";
        this.DD_DeathTimeTwo.DataSource = Ed;
        this.DD_DeathTimeTwo.DataBind();
        this.DD_DeathTimeTwo.SelectedIndex = 0;


        
    }


    public void createNetMu()
    {
        Users_Model userModel=Base_GetUserInfo;
        NetMu_Model model = new NetMu_Model();
        model.MuId = txtNetMuId.Text;
        model.MuName = txtNetMuName.Text;
        model.CreateId = userModel.UserId;
        model.Susername = Txt_UserNa.Text;
        model.Username = Txt_UserName.Text;
        model.OtherName = Txt_otherName.Text;
        model.Picture = Txt_UpImg.Text;
        model.Sex = Radio_Sex.SelectedValue;
        model.Nation = Txt_Race.Text;
        model.Place = Txt_place.Text;
        model.Occupation = Txt_Occu.Text;
        model.Belief = Txt_Belief.Text;
        model.Birthday = Txt_birthyear.Text;
        model.LunarCarlendar_birth = Txt_birthyear.Text;
        model.DepartTime = Txt_DeathTime.Text;
        model.LunarCalendar_depart = Txt_DeathTime.Text;
        model.BirthType = DD_TimeType.SelectedValue;
        if (model.BirthType == TimeType_enum.pub.ToString())
        {
            model.Birthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
            if (StringHelper.isDatetime_Bool(model.Birthday))
            {
                model.LunarCarlendar_birth = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.Birthday));
            }

        }
        else
        {
            model.LunarCarlendar_birth = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
        }



        model.DepartType = DD_DeathType.SelectedValue;

        if (model.DepartType == TimeType_enum.pub.ToString())
        {
            model.DepartTime = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(model.DepartTime))
            {
                model.LunarCalendar_depart = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.DepartTime));
            }
        }
        else
        {
            model.LunarCalendar_depart = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());
        }


        model.Surname_Two = Txt_SnameTwo.Text;
        model.Username_Two = Txt_unameTwo.Text;
        model.OtherName_Two = Txt_OtherNameTwo.Text;
        model.Picture_Two = Txt_UppicTwo.Text;
        model.Sex_two = ddlSexTwo.SelectedValue;
        model.Nation_two = Txt_RaceTwo.Text;
        model.Place_Two = Txt_placeTwo.Text;
        model.Occupation_Two = Txt_OccuTwo.Text;
        model.Belief_Two = Txt_BeliefTwo.Text;
        model.Birthday_Two = Txt_birthTwo.Text;
        model.LunarCalendar_birth_Two = Txt_birthTwo.Text;
        model.DepartTime_Two = Txt_DeathTimeTwo.Text;
        model.LunarCalendar_depart_Two = Txt_DeathTimeTwo.Text;


        model.BirthType_Two = Txt_birChangeTwo.SelectedValue;
        if (model.BirthType_Two == TimeType_enum.pub.ToString())
        {
            model.Birthday_Two = StringHelper.HtmlEncrypt(Txt_birthTwo.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(model.Birthday_Two))
            {
                model.LunarCalendar_birth_Two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.Birthday_Two));
            }
        }
        else
        {
            model.LunarCalendar_birth_Two = StringHelper.HtmlEncrypt(Txt_birthTwo.Text.ToString().Trim());
        }

        model.DepartType_Two = DD_DeathTimeTwo.SelectedValue;

        if (model.DepartType_Two == TimeType_enum.pub.ToString())
        {
            model.DepartTime_Two = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(model.DepartTime_Two))
            {
                model.LunarCalendar_depart_Two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.DepartTime_Two));
            }

        }
        else
        {
            model.LunarCalendar_depart_Two = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());
        }

        model.Readme = WebEditor1.Text;
        model.MuType = StringHelper.isNumber_int(ddlMuType.SelectedValue);
        model.MusicId = 0;
        model.CreateDate = DateTime.Now;
        if (Request["selProvice"] != null)
        {
            model.Province = StringHelper.isNumber_int(Request["selProvice"].ToString());

        }
        if (model.Province > 0)
        {
            if (Request["selCity"] != null)
            {
                model.City = StringHelper.isNumber_int(Request["selCity"].ToString());
            }
        }
        model.Hot = 0;

        model.NetIp = StringHelper.Ip();
        model.IsCheck = 0;
        model.IsDel = 0;

        model.TemplateId = StringHelper.isNumber_int(Request["templateId"]);


        bll.AddNetMu(model);
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        createNetMu();
        DrePoint();
        ResultStr("NetMuList.aspx","创建成功!");
    }
    protected void ddlMuType_SelectedIndexChanged(object sender, EventArgs e)
    {
        
       int sel=Convert.ToInt32(ddlMuType.SelectedValue.ToString());
       if (sel == (int)NetMuTypeEnum.单人墓)
       {
           TwoPanel.Visible = false;
           int Point = (int)Base_GetUserInfo.Point;
           if (Point < configInfo.OnegravestonePoint)
           {
               //ResultStr("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "您的可用点数:" + Point + ",创建双人墓需要" + bll.NeedPointCreateNetMu(NetMuTypeEnum.双人墓) + "点,请您及时充值");
               ResultStr("Default.Aspx", "您的可用点数:" + Point + ",创建公单人墓至少需要" + configInfo.OnegravestonePoint + "点,请您及时充值");
           }
       }
       else if (sel== (int)NetMuTypeEnum.双人墓)
       {
           TwoPanel.Visible = true;
           int Point =(int) Base_GetUserInfo.Point;
           if (Point < configInfo.TwoGravestonePoint)
           {
               //ResultStr("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "您的可用点数:" + Point + ",创建双人墓需要" + bll.NeedPointCreateNetMu(NetMuTypeEnum.双人墓) + "点,请您及时充值");
               ResultStr("Default.Aspx", "您的可用点数:" + Point + ",创建公双人墓至少需要" + configInfo.TwoGravestonePoint + "点,请您及时充值");
           }
       }
    }

    protected string TemplateList()
    { 
        int Types=StringHelper.isNumber_int(ddlMuType.SelectedValue);
        int createNeedPoint=0;
        switch(Types)
        {
            case (int)NetMuTypeEnum.单人墓:
                createNeedPoint=configInfo.OnegravestonePoint;
                break;
            case (int)NetMuTypeEnum.双人墓:
                createNeedPoint=configInfo.TwoGravestonePoint;
                break;
        }
        Temple temple = new Temple();
        return temple.TemplateList(Channel_Type.gravestone, Types,(int) Base_GetUserInfo.Point, createNeedPoint);
    }

    /// <summary>
    /// 扣除点数
    /// </summary>
    protected void DrePoint()
    {
        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);


        int CreateCount = 0;
        int sel = Convert.ToInt32(ddlMuType.SelectedValue.ToString());
        if (sel == (int)NetMuTypeEnum.单人墓)
        {
            CreateCount = configInfo.OnegravestonePoint;
        }
        else if (sel == (int)NetMuTypeEnum.双人墓)
        {
            CreateCount = configInfo.TwoGravestonePoint;
        }
        if (UserId != 0)
        {
            if ((TemplatePoint + CreateCount) <= Base_GetUserInfo.Point)
            {
                Temple_Model TmpMd = TempleBll.CreateNew.GetModel(TempId);
                Point_Log_Model model = new Point_Log_Model();
                model.NetIp = StringHelper.Ip();
                model.Point = TemplatePoint + CreateCount;
                if (TemplatePoint != 0)
                {
                    model.Readme = "创建公墓扣除点数:" + CreateCount + "点,选择模板[" + TmpMd.TempleName+ "]扣除" + TemplatePoint + "点";
                }
                else
                {
                    model.Readme = "创建公墓扣除点数:" + CreateCount + "点,选择了免费模板[" + TmpMd.TempleName+ "]";
                }

                model.UserId = UserId;
                model.UserType = (int)UserTypeEnum.User;
                model.Addtime = DateTime.Now;

                Point_Log_Bll.CreateNew.Add(model);
            }
            else
            {
                Response.Write("<script>alert('无法创建,用户点数不足(需要" + TemplatePoint + CreateCount + "点,您只有" + Base_GetUserInfo.Point + "点)!');location.href='CreateNetMu.aspx';</script>");
                return;
            }
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "请您登录", true);
        }
    }
    
}
